package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.itheima.domain.Category;
import com.itheima.domain.Employee;
import com.itheima.service.CatgoryService;
import com.itheima.tool.R;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("category")
@Slf4j
public class CatgoryController {

    @Autowired
    CatgoryService catgoryService;

    @GetMapping("/page")
    public R getCateByPage(Integer page, Integer pageSize) {
        Page pageinfo = new Page(page, pageSize);
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.orderByAsc(Category::getSort);
        Page page1 = catgoryService.page(pageinfo,lqw);
        return R.success(page1);

    }

    @DeleteMapping
    public R deleteCateById(Long id) {
        catgoryService.remove(id);
        log.info("删除完成");
        return R.success();
    }



    @PostMapping
    public R AddCategory(HttpServletRequest request, @RequestBody Category category) {

        catgoryService.save(category);
        return R.success();
    }

    @PutMapping
    public R updateCategory(HttpServletRequest request, @RequestBody Category category) {

        catgoryService.updateById(category);
        return R.success();
    }


    @GetMapping("/list")
    public R getType(Integer type) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Category::getType, type);
        List<Category> list = catgoryService.list(lqw);
        return R.success(list);
        
    }

}
